<template>
  <el-dialog title="新增员工" :visible.sync="showDialog" @close="reset">
    <el-form ref="form" :model="employee" label-width="80px" class="employee-form" label-position="left" :rules="rules">
      <el-form-item label="名称" prop="username">
        <el-input v-model="employee.username" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="employee.mobile" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="employee.timeOfEntry" type="date" placeholder="选择日期" class="form-pattern" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="employee.formOfEmployment" placeholder="请选择" class="form-pattern">
          <el-option v-for="item in hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="employee.workNumber" />
      </el-form-item>
      <el-form-item ref="formDepartment" label="部门" prop="departmentName">
        <el-input v-model="employee.departmentName" @focus="selectDepart" />
        <!-- 添加部门树 -->
        <el-tree v-if="showDepart" :data="departmentList" :props="defaultProps" @node-click="confirmDepart" />
      </el-form-item>
      <el-form-item label="转正日期" prop="correctionTime">
        <el-date-picker v-model="employee.correctionTime" type="date" placeholder="选择日期" class="form-pattern" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="showDialog=false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { searchSubDepart } from '@/utils'
import { addEmployee } from '@/api/employee'
// 导入枚举
import Enum from '@/api/constant/employees'
export default {
  data() {
    return {
      hireType: Enum.hireType,
      showDialog: false,
      employee: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      showDepart: false,
      departmentList: [],
      defaultProps: { label: 'name' },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 4, message: '长度在 2 到 4个字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3456789]\d{9}$/, message: '手机号格式错误', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '请选择聘用模式', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '请输入员工工号', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '选择员工入职时间', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '选择员工转正时间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    open() {
      // 显示弹窗
      this.showDialog = true
    },
    // 显示部门
    async selectDepart() {
      // 获取部门
      const { depts } = await getDepartments()
      // 将部门转成部门树
      this.departmentList = searchSubDepart(depts, '')
      // 显示部门
      this.showDepart = true
    },
    // 选中部门
    confirmDepart(item) {
      // 关闭弹窗
      this.showDepart = false
      // 选择部门回填弹框
      this.employee.departmentName = item.name
      // 手动触发校验规则
      this.$refs.form.validateField('departmentName')
    },
    // 保存
    async save() {
      await this.$refs.form.validate()
      await addEmployee(this.employee)
      this.$message.success('保存成功')
      this.$emit('reload')
      this.showDialog = false
    },
    // 重置表单
    reset() {
      this.$refs.form.resetFields()
      this.showDepart = false
    }
  }
}
</script>

<style lang="scss" scoped>
.employee-form{
  width: 50%;
  margin: auto;
}
.form-pattern{
  width: 100%;
}
</style>
